﻿using System.Collections.Generic;
using System.Linq;
using IGM.DataProvider;
using IGM.Framework.Constants;

namespace IGM.Business
{
    public partial class Business
    {
        #region -- Get Group Student --

        /// <summary>
        /// This function shows list of Student.
        /// </summary>
        /// <returns></returns>
        public List<StudentLecturer> GetLecturerByStudentId(int studentId)
        {
            return DataContex != null && studentId > 0
                        ? DataContex.StudentLecturers.Where(m => m.IdStudent == studentId).ToList() : null;
        }

        public List<StudentLecturer> GetStudentByLecturerId(int lecturerId)
        {
            return DataContex != null && lecturerId > 0
                        ? DataContex.StudentLecturers.Where(m => m.IdLecturer == lecturerId).ToList() : null;
        }

        public List<StudentLecturer> GetStudentLecturerByMajorId(int major)
        {
            return DataContex != null && major > 0
                        ? DataContex.StudentLecturers.Where(m => m.StudentGraduation.Student.IdMajor == major).ToList()
                        : null;
        }

        public List<StudentLecturer> GetStudentNeedLecturer(int majorId)
        {
            return DataContex != null && majorId > 0
                        ? DataContex.StudentLecturers.Where(m => m.StudentGraduation.Student.IdMajor == majorId && m.IdLecturer == 1).ToList()
                        : null;
        }
      
        #endregion       

        #region -- Add Student Lecturer--

        /// <summary>
        /// 
        /// </summary>
        /// <param name="studentlecturer"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool AddStudentLecturerValidator(StudentLecturer studentlecturer, out string message)
        {
            if (DataContex.StudentLecturers.Any(f => f.IdStudent == studentlecturer.IdStudent && f.IdLecturer == studentlecturer.IdLecturer))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, "Giảng viên hướng dẫn sinh viên");
                return false;
            }

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// This function add a new facutly into list.
        /// </summary>
        public void AddStudentLecturer(StudentLecturer stsudentlecturer)
        {
            DataContex.StudentLecturers.AddObject(stsudentlecturer);
            DataContex.SaveChanges();
        }

        public void AutoStudentLecturer(StudentGraduation studentGraduation)
        {           
            var studentLecturer = new StudentLecturer
            {
                IdStudent = studentGraduation.Id,
                IdLecturer = 1
            };
            AddStudentLecturer(studentLecturer);
        }
        #endregion

        #region -- Edit Student Lecturer --


        public bool EditStudentLecturerValidator(StudentLecturer studentlecturer, out string message)
        {
            var id = studentlecturer.Id;

            if (DataContex.StudentLecturers.Any(f => f.IdStudent == studentlecturer.IdStudent && f.IdLecturer == studentlecturer.IdLecturer && f.Id != id))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, "Giảng viên hướng dẫn sinh viên");
                return false;
            }
            message = string.Empty;
            return true;
        }

        public bool EditStudentLecturer(StudentLecturer studentleturer)
        {
            var id = studentleturer.Id;
            var updateStudentLecturer = DataContex.StudentLecturers.SingleOrDefault(f => f.Id == id);

            if (updateStudentLecturer == null)
                return false;

            updateStudentLecturer.IdLecturer = studentleturer.IdLecturer;

            DataContex.SaveChanges();
            return true;
        }

        #endregion

        #region -- Delete Student Lecturer --

        public bool DeleteStudentLecturerValidator(IEnumerable<StudentLecturer> students, out string message)
        {
            const string intialize = "Giảng viên hướng dẫn: ";
            var itemsCannotIdentify = (from group1 in students
                                       let id = group1.Id
                                       where !DataContex.StudentLecturers.Any(m => m.Id == id)
                                       select group1).Aggregate(intialize, (current, group1) => current + (group1.IdStudent + ", "));

            if (itemsCannotIdentify != intialize)
            {
                message = string.Format(ConstantMessages.SomeItemsCanNotIdentify, itemsCannotIdentify.TrimEnd(','));
                return false;
            }                 

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// Delete object
        /// </summary>
        public void DeleteStudentLecturer(List<StudentLecturer> students)
        {
            for (var i = students.Count - 1; i >= 0; i--)
            {
                var id = students[i].Id;
                var deleteStudent = DataContex.StudentLecturers.Single(m => m.Id == id);
                DataContex.StudentLecturers.DeleteObject(deleteStudent);
            }
            DataContex.SaveChanges();
        } 
     
        #endregion

    }
}